package com.SmartCare.service.impl.homeInfo;

import com.SmartCare.pojo.homeInfo.CareHomeContacts;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.SmartCare.common.enums.Constants;
import com.SmartCare.common.Result;
import com.SmartCare.common.exception.ServiceException;
import com.SmartCare.service.homeInfo.ContactsService;
import com.SmartCare.mapper.homeInfo.CareHomeContactsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 养老院联系方式表 服务实现类
 */
@Service
public class ContactsServiceImpl extends ServiceImpl<CareHomeContactsMapper, CareHomeContacts> implements ContactsService {

    @Override
    public Result getContact(Long id) {
        QueryWrapper<CareHomeContacts> qw = new QueryWrapper<>();
        qw.eq("id", id);
        CareHomeContacts one = getOne(qw);
        if (one == null) {
            throw new ServiceException(Constants.CODE_400, "不存在对应的养老院联系方式");
        }
        return Result.success(one);
    }

    @Override
    public Result add(CareHomeContacts careHomeContacts) {
        //通过雪花算法工具生成id
        long id = IdWorker.getId(careHomeContacts);
        careHomeContacts.setId(id);
        save(careHomeContacts);
        return Result.success(Long.toString(id));
    }

    @Override
    public Result edit(CareHomeContacts careHomeContacts) {
        updateById(careHomeContacts);
        return Result.success();
    }
}
